import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

public class WriterLock implements AbstractLock {


	Semaphore writterIn = new Semaphore(1);

	Semaphore readersIn = new Semaphore(1);
	
	Integer val = 0;

	@Override
	public void readerIn() {
		try {
			writterIn.acquire();

			synchronized (val) {
				val = val - 1;
				readersIn.drainPermits();
			}
			
			
			writterIn.release();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	public  void readerout() {
		
		synchronized (val) {
			val = val -1;
			if(val < 1){
				readersIn.release();
			}
		}
		
	}

	@Override
	public void witerIn() {
		
		try {
			writterIn.acquire();
			readersIn.acquire();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		

		

	}

	@Override
	public void writerout() {
		readersIn.release();
		writterIn.release();
	}

}
